package com.demo.core.service.base.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.transaction.annotation.Transactional;

import com.demo.core.dao.ConfigDao;
import com.demo.core.dao.ConfigItemDao;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.core.model.Config;
import com.demo.core.model.ConfigItem;
import com.demo.core.service.base.ConfigService;
/**
 * 站点配置服务实现类
 * 
 * @author ryuu.kk
 *
 */
public class ConfigServiceImpl implements ConfigService {

	/**
	 * 配置数据访问层Dao
	 */
	private ConfigDao configDao;
	
	/**
	 * 配置Item的数据访问层Dao
	 */
	private ConfigItemDao configItemDao;
	
	@Override
	@Transactional
	public Long save(Config config) {
		Long id = configDao.insert(config);
		List<ConfigItem> configItems = config.getConfigItemList();
		if (configItems != null) {
			for (ConfigItem item : configItems) {
				item.setConfigId(id);
				//key
				String key = item.getKey();
				if (key == null || key.trim().length() == 0) {
					UUID uuid = UUID.randomUUID();
					key = uuid.toString().replaceAll("-", "");
					item.setKey(key);
				}
				//status
				String status = item.getStatus();
				if (status == null || status.trim().length() == 0) {
					item.setStatus(ConfigItem.STATUS_NORMAL);
				}
				//i18n
				String i18n = item.getI18n();
				if (i18n == null || i18n.trim().length() == 0) {
					item.setI18n(config.getI18n());
				}
				configItemDao.insert(item);
			}
		}
		return id;
	}
	
	@Override
	public boolean exists(Long id) {
		return configDao.exists(id);
	}
	
	@Override
	public void delete(Long siteId, Long id) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("configId", id);
		params.put("siteId", siteId);
		configItemDao.delete(params);
		configDao.delete(id);
	}
	
	@Override
	public void updateStatus(Long id, boolean enabled, String status) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		Config config = new Config();
		config.setEnabled(enabled);
		config.setStatus(status);
		configDao.updateStatus(config, params);
	}
	
	@Override
	public void update(Long id, String title, String value) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		Config config = new Config();
		config.setTitle(title);
		config.setValue(value);
		configDao.update(config, params);
	}

	@Override
	public void update(Long id, Config config) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		configDao.update(config, params);
		// 更新子项信息
		List<ConfigItem> configItemList = config.getConfigItemList();
		params.clear();
		for (ConfigItem item : configItemList) {
			params.put("id", item.getId());
			configItemDao.update(item, params);
		}
	}
	
	@Override
	public Config find(Long configId) {
		return configDao.select(configId);
	}

	@Override
	public Config find(Long siteId, String name) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("name", name);
		params.put("siteId", siteId);
		List<Config> configList = configDao.select(params);
		if (configList != null && configList.size() > 0) {
			return configList.get(0);
		}
		return null;
	}

	@Override
	public List<Config> findAll(Long siteId) {
		return findAll(siteId, null);
	}

	@Override
	public List<Config> findAll(Long siteId, String i18n) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("siteId", siteId);
		params.put("i18n", i18n);
		return configDao.select(params);
	}

	@Override
	public Config query(Long id) {
		return configDao.query(id);
	}
	
	@Override
	public Config query(Long siteId, String name) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", name);
		params.put("siteId", siteId);
		List<Config> configList = configDao.query(params);
		if (configList.size() > 0) {
			return configList.get(0);
		}
		return null;
	}

	@Override
	public List<Config> queryAll(Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("siteId", siteId);
		return configDao.query(params);
	}
	
	@Override
	public List<Config> findAll(String name) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("name", name);
		return configDao.query(params);
	}

	@Override
	public List<Config> queryAll(Long siteId, String i18n) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		params.put("i18n", i18n);
		return configDao.query(params);
	}
	
	@Override
	public List<Config> queryList(Long siteId, String type, Boolean enabled) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		params.put("type", type);
		params.put("enabled", enabled);
		return configDao.query(params);
	}
	
	@Override
	public List<Config> queryList(Long siteId, String type) {
		return queryList(siteId, type, null);
	}

	@Override
	public ResultSearchResult<Config> search(Long siteId, String type,
			int firstResult, int maxResults) {
		SearchArgumentSupport sas = new SearchArgumentSupport(firstResult, maxResults);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		params.put("type", type);
		ResultSearchResult<Config> result = configDao.search(params, sas);
		return result;
	}
	
	public void setConfigDao(ConfigDao configDao) {
		this.configDao = configDao;
	}

	public void setConfigItemDao(ConfigItemDao configItemDao) {
		this.configItemDao = configItemDao;
	}
}